package com.mtnz.njk.Service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.GexinUtil.PushToSingle;
import com.mtnz.njk.Mapper.*;
import com.mtnz.njk.Util.ExcelUtils;
import com.mtnz.njk.Util.NewDateUtil;
import com.mtnz.njk.entity.*;
import com.mtnz.njk.model.EveryDayExcelModel;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @Description: app后台管理用户模块服务层
 * @Author: FBL
 * @CreateDate: 2019\8\19 0019 10:07
 * @Version: 1.0
 */
@Service
public class UserManagerService {

    @Autowired
    NjkUserMapper userMapper;

    @Autowired
    NjkUserInfoMapper userInfoMapper;

    @Autowired
    NjkUserAddressMapper userAddressMapper;

    @Autowired
    NjkUserCropMapper userCropMapper;

    @Autowired
    ProductCropMapper productCropMapper;

    @Autowired
    NjkUserServiceMapper userServiceMapper;

    @Autowired
    ProductPriceAuthorityMapper productPriceAuthorityMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponTypeMapper couponTypeMapper;

    @Autowired
    UserManagerMapper userManagerMapper;

    @Autowired
    CountCommunitMapper countCommunitMapper;

    @Autowired
    NjkUserManageMapper njkUserManageMapper;

    @Autowired
    ApplyVipMapper applyVipMapper;

    /**
     * 用户列表
     *
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult listUserInfo(String serviceId, Integer isConnection, Integer isVIP, String key, Integer pageNum, Integer pageSize) throws Exception {

        if ((key != null && !("").equals(key)) || isConnection != null || isVIP != null) {

            // 查询用户信息
            PageHelper.startPage(pageNum, pageSize);
            List<NjkUserEntity> njkUserEntities = njkUserManageMapper.listUserInfo(serviceId, isConnection, isVIP, key);

            PageInfo<NjkUserEntity> njkUserEntityPageInfo = new PageInfo<>(njkUserEntities);
            List<NjkUserEntity> userList = njkUserEntityPageInfo.getList();
            // 查询用户扩展信息
            List<NjkUserInfoEntity> njkUserInfoEntities = userInfoMapper.selectAll();

            for (NjkUserEntity njkUserEntity : userList) {
                for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                    if (njkUserEntity.getUserId().equals(njkUserInfoEntity.getUserId())) {
                        // 将用户扩展信息置入用户中
                        njkUserEntity.setUserInfoEntity(njkUserInfoEntity);
                    }
                }
            }

            // 查询用户作物信息
            List<NjkUserCropEntity> njkUserCropEntities = userCropMapper.selectAll();
            for (NjkUserEntity njkUserEntity : userList) {
                ArrayList<NjkUserCropEntity> userCrops = new ArrayList<>();
                for (NjkUserCropEntity njkUserCropEntity : njkUserCropEntities) {
                    if (njkUserEntity.getUserId().equals(njkUserCropEntity.getUserId())) {
                        // 查询产品作物信息
                        Example productCropExample = new Example(ProductCropEntity.class);
                        productCropExample.createCriteria().andEqualTo("userCropId", njkUserCropEntity.getUserCropId());
                        List<ProductCropEntity> productCropEntities = productCropMapper.selectByExample(productCropExample);
                        njkUserCropEntity.setProductCropEntity(productCropEntities);
                        userCrops.add(njkUserCropEntity);
                    }
                }
                njkUserEntity.setUserCropEntity(userCrops);
            }

            // 查询所有负责人信息
            List<NjkUserServiceEntity> njkUserServiceEntities = userServiceMapper.selectAll();
            for (NjkUserEntity njkUserEntity : userList) {
                for (NjkUserServiceEntity njkUserServiceEntity : njkUserServiceEntities) {
                    if (njkUserEntity.getServiceId().equals(njkUserServiceEntity.getServiceId())) {
                        njkUserEntity.setServiceEntity(njkUserServiceEntity);
                    }
                }
            }
            return new ResponseResult(ResponseMessage.OK, njkUserEntityPageInfo);
        } else {
            PageHelper.startPage(pageNum, pageSize);
            // 查询用户基本信息
            List<NjkUserEntity> njkUserEntities = userMapper.selectAll();
            PageInfo<NjkUserEntity> njkUserEntityPageInfo = new PageInfo<>(njkUserEntities);
            List<NjkUserEntity> userList = njkUserEntityPageInfo.getList();

            // 查询用户扩展信息
            List<NjkUserInfoEntity> njkUserInfoEntities = userInfoMapper.selectAll();
            for (NjkUserEntity njkUserEntity : userList) {
                for (NjkUserInfoEntity njkUserInfoEntity : njkUserInfoEntities) {
                    if (njkUserEntity.getUserId().equals(njkUserInfoEntity.getUserId())) {
                        // 将用户扩展信息置入用户中
                        njkUserEntity.setUserInfoEntity(njkUserInfoEntity);
                    }
                }
            }

            // 查询用户作物信息
            List<NjkUserCropEntity> njkUserCropEntities = userCropMapper.selectAll();

            for (NjkUserEntity njkUserEntity : userList) {
                ArrayList<NjkUserCropEntity> userCrops = new ArrayList<>();
                for (NjkUserCropEntity njkUserCropEntity : njkUserCropEntities) {
                    if (njkUserEntity.getUserId().equals(njkUserCropEntity.getUserId())) {
                        // 查询产品作物信息
                        Example productCropExample = new Example(ProductCropEntity.class);
                        productCropExample.createCriteria().andEqualTo("userCropId", njkUserCropEntity.getUserCropId());
                        List<ProductCropEntity> productCropEntities = productCropMapper.selectByExample(productCropExample);
                        njkUserCropEntity.setProductCropEntity(productCropEntities);
                        userCrops.add(njkUserCropEntity);
                    }
                }
                njkUserEntity.setUserCropEntity(userCrops);
            }

            // 查询所有负责人信息
            List<NjkUserServiceEntity> njkUserServiceEntities = userServiceMapper.selectAll();
            for (NjkUserEntity njkUserEntity : userList) {
                for (NjkUserServiceEntity njkUserServiceEntity : njkUserServiceEntities) {
                    if (njkUserEntity.getServiceId().equals(njkUserServiceEntity.getServiceId())) {
                        njkUserEntity.setServiceEntity(njkUserServiceEntity);
                    }
                }
            }
            return new ResponseResult(ResponseMessage.OK, njkUserEntityPageInfo);
        }
    }

    /**
     * 修改用户信息
     *
     * @param userEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateUserInfo(NjkUserEntity userEntity) throws Exception {

        if (userEntity.getUserId() == null || ("").equals(userEntity.getUserId())) {
            return new ResponseResult(ResponseMessage.NULL_REQUEST, "userId不能为空");
        }
        String userId = userEntity.getUserId();
        // 修改用户基本信息
        userMapper.updateByPrimaryKeySelective(userEntity);
        NjkUserEntity user = userMapper.selectByPrimaryKey(userId);

        // 修改用户地址信息
        if (userEntity.getUserAddressEntity() != null) {
            // 原来有地址信息
            if (userEntity.getUserAddressEntity().getAddressId() != null) {
                NjkUserAddressEntity userAddressEntity = userEntity.getUserAddressEntity();
                userAddressEntity.setName(user.getRealityName());
                userAddressMapper.updateByPrimaryKeySelective(userAddressEntity);
            } else {
                // 没有的话新增
                NjkUserAddressEntity userAddressEntity = userEntity.getUserAddressEntity();
                String id = IdUtil.simpleUUID();
                userAddressEntity.setAddressId(id);
                userAddressEntity.setDefaultAddress(1);
                userAddressEntity.setPhone(user.getPhone());
                userAddressEntity.setUserId(userId);
                userAddressMapper.insertSelective(userAddressEntity);
            }
        }
        // 修改用户扩展信息（job）
        if (userEntity.getUserInfoEntity() != null) {
            // 原来有用户扩展信息
            if (userEntity.getUserInfoEntity().getUserInfoId() != null) {
                NjkUserInfoEntity userInfoEntity = userEntity.getUserInfoEntity();
                userInfoEntity.setVip(user.getVip());
                userInfoMapper.updateByPrimaryKeySelective(userInfoEntity);
            } else {
                // 没有新增
                NjkUserInfoEntity userInfoEntity = userEntity.getUserInfoEntity();
                userInfoEntity.setSex("0");
                userInfoEntity.setYear(DateUtil.year(new Date()) + "");
                userInfoEntity.setMonth(DateUtil.month(new Date()) + "");
                userInfoEntity.setDay(DateUtil.today());
                userInfoEntity.setPhone(user.getPhone());
                userInfoEntity.setUserId(userId);
                userInfoEntity.setVip(user.getVip());
                userInfoMapper.insertSelective(userInfoEntity);
            }
        }
        return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 修改用户作物以及与之关联的产品信息
     *
     * @param userCropEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateUserCrop(NjkUserCropEntity userCropEntity) throws Exception {
        // 修改用户作物信息
        userCropMapper.updateByPrimaryKeySelective(userCropEntity);
        // 修改与之关联的产品信息
        if (userCropEntity.getProductCropEntity() != null && userCropEntity.getProductCropEntity().size() > 0) {
            List<ProductCropEntity> productCropEntity = userCropEntity.getProductCropEntity();
            for (ProductCropEntity productCrop : productCropEntity) {
                if (productCrop.getProductCropId() != null) {
                    productCropMapper.updateByPrimaryKeySelective(productCrop);
                } else {
                    // 新增作物产品
                    productCrop.setCreateTime(DateUtil.now());
                    productCropMapper.insertSelective(productCrop);
                }
            }
        }
        return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 新增用户作物以及相关产品
     *
     * @param userCropEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult insertUserCrop(NjkUserCropEntity userCropEntity) throws Exception {
        // 插入作物
        userCropEntity.setCreateTime(new Date());
        int i = userCropMapper.insertSelective(userCropEntity);
        // 查询最近添加的作物id
        Integer userCropId = userCropMapper.getUserCropId();
        if (userCropEntity.getProductCropEntity() != null && userCropEntity.getProductCropEntity().size() > 0) {
            List<ProductCropEntity> productCropEntity = userCropEntity.getProductCropEntity();
            for (ProductCropEntity cropEntity : productCropEntity) {
                // 插入作物产品
                cropEntity.setUserCropId(userCropId);
                cropEntity.setCreateTime(DateUtil.now());
                productCropMapper.insertSelective(cropEntity);
            }
        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 删除用户作物或者作物下产品信息
     *
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult delUserCrop(Integer userCropId, Integer productCropId) throws Exception {
        int i = 0;
        if (userCropId != null) {
            // 删除用户下的作物
            i += userCropMapper.deleteByPrimaryKey(userCropId);
            // 删除作物下的产品
            Example productCropExample = new Example(ProductCropEntity.class);
            productCropExample.createCriteria().andEqualTo("userCropId", userCropId);
            i += productCropMapper.deleteByExample(productCropExample);
        }
        // 删除单个作物产品
        if (productCropId != null) {
            i += productCropMapper.deleteByPrimaryKey(productCropId);
        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 用户商品权限的查看
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public ResponseResult productPriceAuthority(String userId, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        Example priceAuthorityExample = new Example(ProductPriceAuthorityEntity.class);
        priceAuthorityExample.createCriteria().andEqualTo("userId", userId);
        List<ProductPriceAuthorityEntity> productPriceAuthorityEntities = productPriceAuthorityMapper.selectByExample(priceAuthorityExample);
        PageInfo<ProductPriceAuthorityEntity> productPriceAuthorityEntityPageInfo = new PageInfo<>(productPriceAuthorityEntities);
        List<ProductPriceAuthorityEntity> priceAuthorityList = productPriceAuthorityEntityPageInfo.getList();

        Iterator<ProductPriceAuthorityEntity> iterator = priceAuthorityList.iterator();
        while (iterator.hasNext()) {
            ProductPriceAuthorityEntity productPriceAuthorityEntity = iterator.next();
            String productId = productPriceAuthorityEntity.getProductId();
            ProductEntity productEntity = productMapper.selectByPrimaryKey(productId);
            if (productEntity != null) {
                productPriceAuthorityEntity.setProductName(productEntity.getProductName());
            } else {
                iterator.remove();
            }
        }
        return new ResponseResult(ResponseMessage.OK, productPriceAuthorityEntityPageInfo);
    }

    /**
     * 删除用户商品权限
     *
     * @param authorityId
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult delProductAuthority(String authorityId) throws Exception {
        int i = productPriceAuthorityMapper.deleteByPrimaryKey(authorityId);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 用户申请商品权限
     *
     * @param applyVipEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult applyProductAuthority(ApplyVipEntity applyVipEntity) throws Exception {
        String applyVipId = IdUtil.simpleUUID();
        applyVipEntity.setApplyVipId(applyVipId);
        applyVipEntity.setApplyDate(DateUtil.now());
        // 审核中
        applyVipEntity.setApplyStatus(4);
        int i = applyVipMapper.insertSelective(applyVipEntity);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 查看用户代金券
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public ResponseResult listUserCoupon(String userId, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        Example couponExample = new Example(CouponEntity.class);
        couponExample.createCriteria().andEqualTo("userId", userId).andEqualTo("delFlag", "0");
        List<CouponEntity> couponEntities = couponMapper.selectByExample(couponExample);
        PageInfo<CouponEntity> couponEntityPageInfo = new PageInfo<>(couponEntities);
        List<CouponEntity> couponList = couponEntityPageInfo.getList();
        for (CouponEntity couponEntity : couponList) {
            CouponTypeEntity couponTypeEntity = couponTypeMapper.selectByPrimaryKey(couponEntity.getCouponTypeId());
            couponEntity.setCouponTypeEntity(couponTypeEntity);
        }

        HashMap<String, Object> result = new HashMap<>();
        // 编辑用，所有优惠券类型
        List<CouponTypeEntity> couponTypeEntities = couponTypeMapper.selectAll();
        result.put("coupon", couponEntityPageInfo);
        result.put("couponType", couponTypeEntities);
        return new ResponseResult(ResponseMessage.OK, result);
    }

    /**
     * 删除用户代金券
     *
     * @param couponId
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult delUserCoupon(Integer couponId) throws Exception {
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponId(couponId);
        couponEntity.setDelFlag(1);
        int i = couponMapper.updateByPrimaryKeySelective(couponEntity);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 用户添加代金券
     *
     * @param couponTypeId
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult insertUserCoupon(String userId, Integer couponTypeId) throws Exception {
        // 查询代金券类型信息
        CouponTypeEntity couponTypeEntity = couponTypeMapper.selectByPrimaryKey(couponTypeId);
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setSubMoney(couponTypeEntity.getSubMoney());
        couponEntity.setCreateTime(new Date());
        couponEntity.setUserId(userId);
        couponEntity.setDelFlag(0);
        couponEntity.setCouponImg(couponTypeEntity.getCouponImg());
        couponEntity.setCouponExplain(couponTypeEntity.getCouponExplain());
        couponEntity.setCouponTypeId(couponTypeId);
        int i = couponMapper.insertSelective(couponEntity);

        NjkUserEntity userEntity = userMapper.selectByPrimaryKey(userId);
        String clientId = userEntity.getClientId();
        if (clientId != null && !("").equals(clientId)) {
            // 透传消息传递给客户端
            PushToSingle pushtoSingle = new PushToSingle();
            pushtoSingle.pushToSingle(clientId, " 通知栏标题", "通知栏内容", 2, "yhq", 2);
        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 用户电话日状态修改
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updatePhoneStatus(String userId, String phoneResult) throws Exception {
        NjkUserEntity userEntity = new NjkUserEntity();
        userEntity.setUserId(userId);
        userEntity.setTopPhoneResult(phoneResult);
        userEntity.setPhoneStatus(1);
        userEntity.setTopPhoneTime(new Date());
        int i = userMapper.updateByPrimaryKeySelective(userEntity);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 用户信息导出表格
     *
     * @return
     * @throws Exception
     */
    public ResponseResult userOutExcel(HttpServletResponse response) throws Exception {
        // 查询用户信息
        List<HashMap<String, String>> lists = userManagerMapper.getUserMessage();
        List<String[]> list = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            String[] str = new String[14];
            str[0] = lists.get(i).get("nickName").toString();
            str[1] = lists.get(i).get("phone").toString();
            str[2] = lists.get(i).get("addressDetail").toString();
            str[3] = lists.get(i).get("cropName").toString();
            str[4] = lists.get(i).get("beginTime").toString();
            str[5] = lists.get(i).get("endTime").toString();
            str[6] = lists.get(i).get("productName").toString();
            str[7] = lists.get(i).get("repositoryNum").toString();
            str[8] = lists.get(i).get("shopTime").toString();
            str[9] = lists.get(i).get("shopCover") + "";
            str[10] = lists.get(i).get("shopCapacity") + "";
            str[11] = lists.get(i).get("topPhoneTime").toString();
            str[12] = lists.get(i).get("topPhoneResult").toString();
            str[13] = lists.get(i).get("remark");
            list.add(str);
        }
        String[] strs = {"客户姓名", "电话", "地区（省/市/县/镇/详细地址）", "种植作物", "萌芽时间", "收获时间", "最佳铺货产品", "库存", "最佳铺货时间", "市场覆盖面积", "预估市场容量", "上次打电话时间", "打电话结果", "备注"};
        ExcelUtils.exportExelMerge(System.currentTimeMillis() + "_njk.xls", strs, list, false, response, new Integer[]{0, 9}, new Integer[]{0, 1, 2, 3, 4, 5, 9, 10, 11, 12}, new Integer[]{}, new Integer[]{});
        return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 用户积分详情
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public ResponseResult listUserIntegral(String userId, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        Example countCommunityExample = new Example(CountCommunityEntity.class);
        countCommunityExample.createCriteria().andEqualTo("userId", userId);
        List<CountCommunityEntity> countCommunityEntities = countCommunitMapper.selectByExample(countCommunityExample);
        // 回复1/提问2/研究院3/购买商品4/分享5/积分兑换6
        PageInfo<CountCommunityEntity> countCommunityEntityPageInfo = new PageInfo<>(countCommunityEntities);
        return new ResponseResult(ResponseMessage.OK, countCommunityEntityPageInfo);
    }

    /**
     * 申请积分变动
     *
     * @param userId
     * @param news
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateUserIntegral(String userId, Integer communityType, String news, Integer integralNum) throws Exception {
        // 增加一条用户积分记录
        CountCommunityEntity countCommunityEntity = new CountCommunityEntity();
        countCommunityEntity.setUserId(userId);
        countCommunityEntity.setNum(integralNum + "");
        countCommunityEntity.setCommunityType(communityType);
        countCommunityEntity.setCreateTime(new Date());
        countCommunityEntity.setNews(news);
        countCommunityEntity.setAudit(0);
        int i = countCommunitMapper.insertSelective(countCommunityEntity);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 每日运营数据
     *
     * @param status 0 周 1 月 2 年
     * @return
     */
    public ResponseResult dailyDatabase(Integer status) throws Exception {
        //返回模型
        Map<String, Object> resultMap = new HashMap<>();
        //签到
        List<Object> signlist = new ArrayList<>();
        List<Object> tasklist1 = new ArrayList<>();
        List<Object> tasklist2 = new ArrayList<>();
        List<Object> tasklist3 = new ArrayList<>();
        //  List<Object> tasklist4 = new ArrayList<>();
        List<Object> tasklist5 = new ArrayList<>();
        List<Object> taskAlllist = new ArrayList<>();
        //判断周，月，年
        String[] strs;
        strs = status == 0 ? NewDateUtil.getDates(7, ChronoUnit.DAYS, "yyyy-MM-dd") : status == 2 ? NewDateUtil.getDates(12, ChronoUnit.MONTHS, "yyyy-MM") : NewDateUtil.getDates(30, ChronoUnit.DAYS, "yyyy-MM-dd");
        java.util.Map<String, Object> signGatherMap;
        java.util.Map<String, Object> taskAllGatherMap;
        java.util.Map<String, Object> taskGatherMap;
        java.util.Map<String, Object> taskType1;
        java.util.Map<String, Object> taskType2;
        java.util.Map<String, Object> taskType3;
        //   java.util.Map<String, Object> taskType4;
        java.util.Map<String, Object> taskType5;
        try {
            Arrays.sort(strs);
            taskGatherMap = new HashMap<>();
            for (String str : strs) {
                signGatherMap = new HashMap<>();
                taskAllGatherMap = new HashMap<>();
                taskType1 = new HashMap<>();
                taskType2 = new HashMap<>();
                taskType3 = new HashMap<>();
                //      taskType4 = new HashMap<>();
                taskType5 = new HashMap<>();
                //1.签到
                signGatherMap.put("date", str);
                signGatherMap.put("data", status == 2 ? njkUserManageMapper.monthSign(str) : njkUserManageMapper.todaySign(str));
                signlist.add(signGatherMap);
                //2.全部任务
                taskAllGatherMap.put("date", str);
                taskAllGatherMap.put("data", status == 2 ? njkUserManageMapper.monthTaskAll(str) : njkUserManageMapper.todayTaskAll(str));
                taskAlllist.add(taskAllGatherMap);
                //3.每个任务
                taskType1.put("date", str);
                taskType1.put("data", status == 2 ? njkUserManageMapper.monthTask(1, str) : njkUserManageMapper.todayTask(1, str));
                taskType2.put("date", str);
                taskType2.put("data", status == 2 ? njkUserManageMapper.monthTask(2, str) : njkUserManageMapper.todayTask(2, str));
                taskType3.put("date", str);
                taskType3.put("data", status == 2 ? njkUserManageMapper.monthTask(3, str) : njkUserManageMapper.todayTask(3, str));
                /*taskType4.put("date", str);
                taskType4.put("data", status == 2 ?njkUserManageMapper.monthTask(4,str):njkUserManageMapper.todayTask(4,str));*/
                taskType5.put("date", str);
                taskType5.put("data", status == 2 ? njkUserManageMapper.monthTask(5, str) : njkUserManageMapper.todayTask(5, str));
                tasklist1.add(taskType1);
                tasklist2.add(taskType2);
                tasklist3.add(taskType3);
                //     tasklist4.add(taskType4);
                tasklist5.add(taskType5);
            }
            //签到
            taskGatherMap.put("status1", tasklist1);
            //信息补全
            taskGatherMap.put("status2", tasklist2);
            //提问回复
            taskGatherMap.put("status3", tasklist3);
            //研究院
//            taskGatherMap.put("status4", tasklist4);
            //分享
            taskGatherMap.put("status5", tasklist5);
            resultMap.put("sign", signlist);
            resultMap.put("task", taskGatherMap);
            resultMap.put("taskAll", taskAlllist);
            return new ResponseResult(ResponseMessage.OK, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 每日运行数导出excel
     *
     * @param response
     * @param startTime
     * @param endTime
     * @return
     */
    public ResponseResult outputExcleOperator(HttpServletResponse response, String startTime, String endTime) throws Exception {
        try {
            List<String> strs = NewDateUtil.getDays(startTime, endTime);
            //excel模型
            List<String[]> excelModels = new ArrayList<>();
            for (int i = 0; i < strs.size(); i++) {
                String time = strs.get(i);
                List<EveryDayExcelModel> excel = njkUserManageMapper.todaySevenTaskEnd(time);
                for (EveryDayExcelModel everyDayExcelModel : excel) {
                    String[] str = new String[9];
                    str[0] = time;
                    str[1] = everyDayExcelModel.getNickName();
                    str[2] = everyDayExcelModel.getPhone();
                    str[3] = everyDayExcelModel.getRealityName();
                    String vip = everyDayExcelModel.getVip();
                    if (!"0".equals(vip)) {
                        str[4] = "是";
                    }
                    str[4] = "否";
                    String status2 = "";
                    if (everyDayExcelModel.getIntegralStatus().equals("1")) {
                        status2 = "是";
                    } else {
                        status2 = "否";
                    }
                    str[5] = status2;
                    str[6] = everyDayExcelModel.getStatus1();
                    str[7] = everyDayExcelModel.getStatus3();
                    str[8] = everyDayExcelModel.getStatus5();
                    excelModels.add(str);
                }
            }
            String name = System.currentTimeMillis() + "_njk.xls";


            List<String[]> strings = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                if (i == 0) {
                    String[] str = {"", "每日任务完成用户", "", "", "", "", "", "", ""};
                    strings.add(str);
                } else {
                    String[] str = {"", "昵称", "手机号", "姓名", "vip", "完善资料", "每日签到", "提问回复", "分享"};
                    strings.add(str);
                }
            }
            ExcelUtils.createExcelMerge_complexdo(name, strings, excelModels, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return new ResponseResult(ResponseMessage.OK);
    }
}
